<template>
  <div class="home">

    <headers @updateValue="changerouter" :class="{ fixed: isFixed }"></headers>
    <!-- <div class="from">
      <Edit></Edit>
    </div> -->
    <div class="home-new">
      <div class="home-new-left" :class={isEditRoutes:isEditRoute}>
         <router-view></router-view>
      </div>
      <div v-if="!isEditRoute" class="home-new-right">
         <userxx></userxx>
          
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed,watch, onMounted, onBeforeUnmount, h } from 'vue'
// import { Icons } from 'ikicons'
import { useRouter,useRoute } from 'vue-router'
import headers from '@/components/header.vue'
import userxx from '@/components/userxx.vue'
import emitter from '@/function/eventBus'
import { selectprandlable } from '@/api/request/userxx/requestuser'
import { useCounterStore } from '@/store/index.js'

const store = useCounterStore()
const isFixed = ref(false);  // 使用 ref 来定义响应式数据
const isfrst = ref(true)
const router = useRouter()
const route = useRoute()
 console.log(route.name);
//在首页检查指定cookie是否存在
const isEditRoute = ref(true)
watch(
  () => route.name,
  (newName, oldName) => {
     console.log(newName);
   if(newName==='edit') isEditRoute.value=true
   else  isEditRoute.value=false
  }
)
const changerouter=(type)=>{
   if(type=='Edit') {
router.push({
  name: 'edit',
  params: {
    typeby: 'add', // 必须与路径参数名一致
    postid:''
  }
})
return
   }
  if(type!=='shequ')router.push(`/${type}`)
  else router.push('/')
  
}

// 处理滚动事件
const handleScroll = () => {
  store.listenwinscrooll(window.scrollY)
  if (window.scrollY > 100 && isfrst.value) {
    isFixed.value = true
    isfrst.value = false
  } else if (window.scrollY == 0 && !isfrst.value) {
    isFixed.value = false
    isfrst.value = true
  }
};

const selectpublic = async () => {
  
  const res = await selectprandlable({ type: '2' })

  
  if (res.status == 200) {

    store.changepublidata(['permissiontypelist','fromlablelist'],res.data.data)
  }
}
// 在组件挂载后监听滚动事件
onMounted(async () => {

  window.addEventListener('scroll', handleScroll);
  emitter.on('changerouter', changerouter)
  await  selectpublic()
});

// 在组件销毁前移除滚动事件监听器
onBeforeUnmount(() => {
  window.removeEventListener('scroll', handleScroll);
  emitter.off('changerouter', changerouter)
});



</script>

<style>
@import '@/assets/css/home.css';
</style>